package com.morehair.service.wx.impl;

import com.morehair.bean.pojo.*;

import com.morehair.bean.vo.wx.WxSearchVo;
import com.morehair.mapper.KeyWordMapper;
import com.morehair.mapper.SearchHistoryMapper;
import com.morehair.service.wx.WxSearchService;
import com.morehair.utils.GetNowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author  kkw
 * Date  2021/6/9 11:09
 * Version 1.0
 * Description :
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    KeyWordMapper keyWordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchVo getSearch() {
        WxSearchVo wxSearchVo = new WxSearchVo();
        //defaultKeyword
        KeyWordExample keyWordExample = new KeyWordExample();
        KeyWordExample.Criteria criteria = keyWordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<KeyWord> keyWords = keyWordMapper.selectByExample(keyWordExample);
        wxSearchVo.setDefaultKeyword(keyWords.get(0));
        //hotKeywordList
        KeyWordExample keyWordExample1 = new KeyWordExample();
        keyWordExample1.createCriteria().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<KeyWord> keyWords1 = keyWordMapper.selectByExample(keyWordExample1);
        wxSearchVo.setHotKeywordList(keyWords1);
        //historyKeywordList
        User user = GetNowUser.getUser();
        if (user==null){
            wxSearchVo.setHistoryKeywordList(null);
        }else {
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            SearchHistoryExample.Criteria criteria1 = searchHistoryExample.createCriteria();
            criteria1.andDeletedEqualTo(false).andUserIdEqualTo(user.getId());
            List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
            wxSearchVo.setHistoryKeywordList(searchHistories);

        }

        return wxSearchVo;
    }

    @Override
    public List<String> getKeyword(String keyword) {
        KeyWordExample keyWordExample = new KeyWordExample();
        KeyWordExample.Criteria criteria = keyWordExample.createCriteria();
        criteria.andDeletedEqualTo(false).andKeywordLike("%"+keyword+"%");
        List<KeyWord> keyWords = keyWordMapper.selectByExample(keyWordExample);
        User user = GetNowUser.getUser();
        List<String> keyName=new ArrayList<>();
        for (KeyWord keyWord1 : keyWords) {
            keyName.add(keyWord1.getKeyword());
            if (user!=null){
                SearchHistory searchHistory = new SearchHistory();
                searchHistory.setKeyword(keyWord1.getKeyword());
                searchHistory.setUserId(user.getId());
                searchHistory.setFrom(user.getNickname());
                searchHistory.setAddTime(new Date());
                System.out.println(searchHistory);
                searchHistoryMapper.insertSelective(searchHistory);
            }
        }
        return keyName;
    }

    @Override
    public void clearHistory() {
        User user = GetNowUser.getUser();
        if (user == null) {
            return;
        }
        SearchHistory searchHistory = new SearchHistory();
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId()).andDeletedEqualTo(false);
        searchHistory.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(searchHistory,searchHistoryExample);
    }
}
